<template>
  <div class="home-container">
    <!-- 导航栏 -->
    <van-nav-bar class="page-nav-bar" fixed>
      <van-button
        slot="title"
        type="info"
        round
        size="small"
        icon="search"
        class="search-btn"
        to="/search"
      >
        搜索
      </van-button>
    </van-nav-bar>

    <!-- 频道列表 -->
    <van-tabs class="channel-tabs" v-model="active" animated swipeable>
      <van-tab
        :title="channel.name"
        v-for="channel in channels"
        :key="channel.id"
      >
        <!-- 文章列表组件的模板 -->
        <article-list :channel="channel"></article-list>
      </van-tab>
      <div slot="nav-right" class="placeholder"></div>
      <div
        slot="nav-right"
        class="hamburger-btn"
        @click="isChannelEditShow = true"
      >
        <i class="toutiao toutiao-gengduo"></i>
      </div>
    </van-tabs>

    <!-- 频道弹出层 -->
    <van-popup
      v-model="isChannelEditShow"
      closeable
      position="right"
      :style="{ height: '100%', width: '100%' }"
    >
      <channel-edit
        :my-channels="channels"
        :active="active"
        @updata-active="onUpDataActive"
      ></channel-edit>
    </van-popup>
  </div>
</template>

<script>
// 加载获取频道列表的方法
import { getUserChnnels } from '@/api/user'
// 加载文章列表组件
import ArticleList from './components/article-list'
// 加载频道编辑组件
import ChannelEdit from './components/channel-edit'
// 调用vuex的mapState方法
import { mapState } from 'vuex'
// 加载获取本地存储数据的方法
import { getItem } from '@/utils/storage'

export default {
  name: 'HomeIndex',
  components: {
    ArticleList,
    ChannelEdit
  },
  computed: {
    ...mapState(['user'])
  },
  created() {
    this.loadChnnels()
  },
  data() {
    return {
      active: 0,
      channels: [], //频道列表
      isChannelEditShow: false //频道弹出层显示与隐藏
    }
  },
  methods: {
    async loadChnnels() {
      try {
        let channels = []
        if (this.user) {
          // 1.已登录,获取用户频道列表
          const { data } = await getUserChnnels()
          channels = data.data.channels
        } else {
          // 2.未登录,判断是否有本地频道列表的数据
          const localChnnels = getItem('TOUTIAO_CHANNELS')
          // 3.有数据,就拿来用
          if (localChnnels) {
            channels = localChnnels
          } else {
            // 4.没数据,就拿取获取的默认频道列表
            const { data } = await getUserChnnels()
            channels = data.data.channels
          }
        }
        this.channels = channels
      } catch (err) {
        this.$toast('获取频道列表失败!')
      }
    },
    onUpDataActive(index, isChannelEditShow = true) {
      // 更新激活的频道项
      // 子组件传递的索引值关联父组件的active值
      // 父组件更新后子组件通过porps也会跟着发生变化
      this.active = index
      // 关闭弹出层
      this.isChannelEditShow = isChannelEditShow
    }
  }
}
</script>

<style lang="less" scoped>
.home-container {
  padding-top: 174px;
  padding-bottom: 100px;
  /deep/.van-nav-bar__title {
    max-width: unset;
  }
  .van-nav-bar .van-icon {
    color: #fff;
    font-size: 35px;
  }
  .search-btn {
    width: 555px;
    height: 64px;
    background-color: #5abbfb;
    border: none;
    font-size: 28px;
  }
  /deep/ .channel-tabs {
    .van-tabs__wrap {
      position: fixed;
      top: 92px;
      z-index: 1;
      left: 0;
      right: 0;
      height: 82px;
    }
    .van-tab {
      min-width: 200px;
      border-right: 1px solid #edeff3;
      font-size: 30px;
      color: #777;
    }
    .van-tab--active {
      color: #333;
    }
    .van-tabs__nav {
      padding-bottom: 23px;
    }
    .van-tabs__line {
      width: 31px !important;
      height: 6px;
      background-color: #3296fa;
    }
  }
  .placeholder {
    flex-shrink: 0;
    width: 66px;
    height: 82px;
  }
  .hamburger-btn {
    position: fixed;
    right: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    width: 66px;
    height: 82px;
    background-color: #fff;
    opacity: 0.902;
    i.toutiao {
      font-size: 33px;
    }
    &:before {
      content: '';
      position: absolute;
      left: 0;
      width: 1px;
      height: 58px;
      background-image: url(~@/assets/gradient-gray-line.png);
      background-size: contain;
    }
  }
}
</style>